package com.zxy.annotation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author zxy
 * @date 2019-04-28 13:34
 */
public class RequestMappingHandler {
    /**
     * 解析方法注解
     *
     * @param <T>
     * @param clazz
     */
    public static <T> void handle(Class<T> clazz, Object servlet, String uri, HttpServletRequest request, HttpServletResponse response) {
        try {
            for (Method method : clazz.getDeclaredMethods()) {
                RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);

                if (requestMapping != null) {
                    String name = requestMapping.value();
                    if (uri.equals(name)) {
                        //通过反射调用带有此注解的方法
                        method.invoke(servlet, request, response);
                    }
                }
            }
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }

    }
}
